<template>
  <div class="add-org">
    <!-- 详情 -->
    <cvue-dialog
      :dialogVisible="dialogVisible"
      title="编辑"
      :isShowFoot="true"
      dialogWidth="65%"
      @handleOpen="getDetail"
      @closeDialog="closeDialog"
      @confirmDialog="confirmDialog"
      @handleClose="handleClose"
    >
      <div class="dialog-body" slot="dialogBody" style="padding: 0;">
        <div class="selCenter">
          <el-tabs v-model="activeName" type="card">
            <el-tab-pane label="基本信息" name="first">
                 <el-form inline label-width="60px">
                    <el-form-item label="名称" required>
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.name" placeholder="请输入学校名称"></el-input>
                    </el-form-item>
                    <el-form-item label="编码">
                        <el-input class="formWidth" disabled="" type="text" v-model="schoolInfo.internalCode" placeholder="由系统自动生成"></el-input>
                    </el-form-item>
                    <el-form-item label="代理">
                          <treeselect style="width:216px; line-height: 16px;"
                           :maxHeight='200'
                            v-model="agentId"
                            :normalizer='normalizer'
                             placeholder='请选择代理'
                              :options="agentData" />
                      </el-form-item>
                  </el-form>
                  <!-- {{sheng}}--{{shi}}--{{qu}} -->
                  <area-component
                    @areaObj='areaObj'
                    :province="sheng"
                    :city="shi"
                    :county="qu"
                    ref="areIsCity"
                  ></area-component>
                  <el-form inline label-width="60px">
                    <el-form-item label="学年" required>
                          <el-select v-model="schoolInfo.currentSchoolYearId" placeholder="请选择">
                            <el-option
                            v-for="item in schoolYearData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                    <el-form-item label="学段" required>
                          <el-select v-model="schoolInfo.stageId" @change="changeSubject" placeholder="请选择">
                            <el-option
                            v-for="item in stageData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item label="科目" required>
                          <el-select v-model="subjectIds" multiple collapse-tags placeholder="请选择"  @focus="cliSub">
                            <el-option
                            v-for="item in subjectData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                      </el-form-item>
                      
                  </el-form>
                  <el-form inline label-width="60px">
                    <el-form-item label="账号" required>
                          <el-input class="formWidth" type="text" disabled v-model.trim="schoolInfo.adminLogin" placeholder="请输入教务总账号"></el-input>
                      </el-form-item>
                      <el-form-item label="类型" required>
                          <el-select v-model="schoolInfo.schoolTypeCode" placeholder="请选择办学类型">
                              <el-option
                              v-for="item in dict.SchoolType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                      </el-form-item>
                  </el-form>
                  <el-form inline label-width="60px">
                       <el-form-item label="年级" required>
                        <el-select v-model="gradeDistributions" multiple collapse-tags placeholder="请选择所配年级">
                              <el-option
                              v-for="item in gradeData"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id">
                              </el-option>
                          </el-select>
                    </el-form-item>
                  </el-form>
                   <el-form inline label-width="100px">
                    <el-form-item label="学校经纬度">
                     
                        <el-button @click="openMap" type="primary" icon="el-icon-map-location">设置经纬度</el-button>
                        <span v-if="schoolInfo.longitude">经度：{{schoolInfo.longitude}} , 纬度：{{schoolInfo.latitude}}</span>
                    </el-form-item>
                 </el-form>
            </el-tab-pane>
            <!-- ------------------------------------------------------------------------------------------------------- -->
            <el-tab-pane label="详细信息" name="second">
               <el-form inline label-width="150px">
                    <el-form-item label="学校标识码">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.idCode" placeholder="请输入学校标识码"></el-input>
                    </el-form-item>
                    <el-form-item label="地址">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.address" placeholder="请输入学校地址"></el-input>
                    </el-form-item>
                    <el-form-item label="电子邮箱">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.email" placeholder="电子邮箱"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="英文名称">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.englishName" placeholder="请输入学校英文名称"></el-input>
                    </el-form-item>
                    <el-form-item label="学校地址代码">                         
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.addressCode" placeholder="请输入学校地址代码"></el-input>
                    </el-form-item>
                    <el-form-item label="邮政编码">                                              
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.postalCode" maxlength="10" placeholder="请输入学校邮政编码"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="驻地城乡类型代码">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.urbanRuralTypeCode" placeholder="请输入学校驻地城乡类型代码"></el-input>
                    </el-form-item>
                    <el-form-item label="属地教育行政">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.locationAdminDeptName" placeholder="属地管理教育行政部门名称"></el-input>
                    </el-form-item>
                    <el-form-item label="属地教育行政代码">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.locationAdminDeptCode" placeholder="学校属地管理教育行政部门代码"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="所属教育行政">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.deptInChargeName" placeholder="所属主管教育行政部门名称"></el-input>
                    </el-form-item>
                    <el-form-item label="所属教育行政代码">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.deptInChargeCode" placeholder="学校所属主管教育行政部门代码"></el-input>
                    </el-form-item>
                    <el-form-item label="网站主页地址">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.websiteUrl" placeholder="网站主页地址"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form inline label-width="150px">
                    <el-form-item label="少数民族学校">
                        <div class="formWidth">
                             <el-switch v-model="schoolInfo.minoritySchoolFlag"></el-switch>
                        </div>
                    </el-form-item>
                    <el-form-item label="联系电话">
                        <el-input class="formWidth" type="number" v-model.trim="schoolInfo.contactPhoneNum" placeholder="联系电话"></el-input>
                    </el-form-item>
                    <el-form-item label="传真电话">
                        <el-input class="formWidth" type="number" v-model.trim="schoolInfo.faxPhoneNum" placeholder="传真电话"></el-input>
                    </el-form-item>
                  </el-form>
                  <!-- <el-form inline label-width="150px">
                  </el-form> -->
            </el-tab-pane>
            <!-- ------------------------------------------------------------------------------------------------------- -->
            <el-tab-pane label="扩展信息" name="second2">
                <el-form inline label-width="150px">
                    <el-form-item label="校长名字">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.schoolmasterName" placeholder="校长名字"></el-input>
                    </el-form-item>
                    <el-form-item label="校长手机号码">
                        <el-input class="formWidth" type="number" v-model.trim="schoolInfo.schoolmasterCellPhoneNum" placeholder="校长手机号码"></el-input>
                    </el-form-item>
                    <el-form-item label="学校举办者类型">
                        <el-select v-model="schoolInfo.schoolRunnerTypeCode" placeholder="请选择学校举办者类型">
                              <el-option
                              v-for="item in dict.SchoolRunnerType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="附设班类型">
                        <el-select v-model="schoolInfo.attachedClassTypeCode" placeholder="请选择附设班类型">
                              <el-option
                              v-for="item in dict.AttachedClassType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="小学学制">
                        <el-select v-model="schoolInfo.psSchoolingLengthCode" placeholder="请选择小学学制">
                              <el-option
                              v-for="item in dict.PsSchoolingLength"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="初中学制">
                        <el-select v-model="schoolInfo.jhsSchoolingLengthCode" placeholder="请选择初中学制">
                              <el-option
                              v-for="item in dict.JhsSchoolingLength"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="高中学制">
                        <el-select v-model="schoolInfo.shsSchoolingLengthCode" placeholder="请选择高中学制">
                               <el-option
                              v-for="item in dict.ShsSchoolingLength"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="组织机构代码证号">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.orgCodeCertifNum" placeholder="组织机构代码证号"></el-input>
                    </el-form-item>
                    <el-form-item label="建校年份">
                        <el-date-picker
                          v-model="foundYear"
                          type="year"
                          value-format="yyyy"
                          placeholder="选择年">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="校庆日">
                        <el-date-picker
                          v-model="schoolDay"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择年">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="土地产权类型">
                        <el-select v-model="schoolInfo.landPropertyRightTypeCode"  placeholder="请选择土地产权类型">
                              <el-option
                              v-for="item in dict.LandPropertyRightType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="土地证号">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.landCertifNum" placeholder="土地证号"></el-input>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="法人登记证号">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.legalPersonRegCertifNum" placeholder="法人登记证号"></el-input>
                    </el-form-item>
                    <el-form-item label="寄宿制形式">
                        <el-select v-model="schoolInfo.boardingTypeCode" placeholder="请选择寄宿制形式">
                              <el-option
                              v-for="item in dict.BoardingType"
                              :key="item.orderNum"
                              :label="item.valName"
                              :value="item.orderNum">
                              </el-option>
                          </el-select>
                    </el-form-item>
                  
                </el-form>
                
                <el-form inline label-width="150px">
                   
                    <el-form-item label="学校海拔（米）">
                        <el-input class="formWidth" type="number" v-model.trim="schoolInfo.aboveSeaLevel" placeholder="学校海拔高度（米）"></el-input>
                    </el-form-item>
                    <el-form-item label="招生半径（公里）">
                        <el-input class="formWidth" type="number" v-model.trim="schoolInfo.enrollmentRadius" placeholder="招生半径（公里）"></el-input>
                    </el-form-item>
                </el-form>
                <el-form inline label-width="150px">
                    <el-form-item label="主教学语言">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.mainTeachingLanguage" placeholder="主教学语言"></el-input>
                    </el-form-item>
                    <el-form-item label="辅教学语言">
                        <el-input class="formWidth" type="text" v-model.trim="schoolInfo.auxTeachingLanguage" placeholder="辅教学语言"></el-input>
                    </el-form-item>
                    <el-form-item label="学校历史沿革">
                        <el-input class="formWidth" type="textarea" v-model.trim="schoolInfo.schoolHistory" placeholder="学校历史沿革"></el-input>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
          </el-tabs>          
        </div>
      </div>
    </cvue-dialog>

     <amap :dialogVisible='mapVisible' 
            :lng='schoolInfo.longitude'
            :lat='schoolInfo.latitude'
        @close='closeMap' 
        @comfirm='setLngLat'></amap>
  </div>
</template>
<script>
import cvueDialog from '@/components/cvue-dialog'
import areaComponent from '@/components/areaComponent'
import amap from '@/components/map'
export default {
  name: 'editSchool',
  components: {
    cvueDialog,
    areaComponent,
    amap
  },

  data () {
    return {
      mapVisible: false,
      normalizer (node) {
                return {
                    id: node.id,
                    label: node.name,
                    children: node.children.length > 0 ? node.children : 0
                }
            },
       sheng: '',
        shi: '',
        qu: '',
        activeName: 'first',
        subjectIds: [], // 科目
        agentId: null, // 代理
        gradeDistributions: [], // 所配年级
        foundYear: '', // 建校年份
        schoolDay: '', // 校庆日
        areaId: '', // 区域id
        areaData: '', // 省市区数据
        schoolInfo: '',
        runSchool: '',
        subjectData: []
    }
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  mounted () {

  },
  watch: {
    // sheng (newVal, oldVue) {
    //     this.sheng = newVal
    // },
    // shi (newVal, oldVue) {
    //     this.shi = newVal
    // },
    // qu (newVal, oldVue) {
    //     this.qu = newVal
    // }
  },
  computed: {
    gradeData () {
          return this.$store.state.schoolManage.gradeData
      },
      // subjectData () {
      //     return this.$store.state.schoolManage.subjectData
      // },
      dict () {
          return this.$store.state.schoolManage.dict
      },
      stageData () {
          return this.$store.state.schoolManage.stageData
      },
      schoolYearData () {
          return this.$store.state.schoolManage.schoolYearData
      },
      agentData () {
          return this.$store.state.schoolManage.agentData
      }
  },
  methods: {
    openMap () {
        this.mapVisible = true
    },
    closeMap () {
        this.mapVisible = false
    },
    setLngLat (obj) {
      this.schoolInfo.longitude = obj.lng
      this.schoolInfo.latitude = obj.lat
    },
    // 触发事件
    handleClose () {
      this.$emit('handleClose')
    },
    closeDialog () {
      this.$emit('closeDialog')
    },
    confirmDialog () {
      if (!this.schoolInfo.name) {
        this.$message({type: 'error', message: '请输入学校名字'}) 
        return
      }
       if (!this.areaData.provinceId) {
            this.$message({
                type: 'error',
                message: '请选择省'
              }) 
            return
          }
      //     if (!this.areaData.cityId && this.areaData.isZhi) {
      //       this.$message({
      //           type: 'error',
      //           message: '请选择省市区'
      //         }) 
      //       return
      //     }
      //    if (!this.areaData.countyId && !this.areaData.isZhi) {
      //       this.$message({
      //           type: 'error',
      //           message: '请选择省市区'
      //         }) 
      //       return
      //     }
        if (!this.schoolInfo.currentSchoolYearId) {
        this.$message({type: 'error', message: '请选择学年'}) 
        return
      }
      if (!this.schoolInfo.stageId) {
        this.$message({type: 'error', message: '请选择学段'}) 
        return
      }
      if (this.subjectIds.length == 0) {
        this.$message({type: 'error', message: '请选择科目'}) 
        return
      }
      // if (!this.schoolInfo.adminLogin) {
      //   this.$message({type: 'error', message: '请输入教务总账号'}) 
      //   return
      // }
      // if (!this.agentId) {
      //   this.$message({type: 'error', message: '请选择代理商'}) 
      //   return
      // }
      if (!this.schoolInfo.schoolTypeCode) {
        this.$message({type: 'error', message: '请选择办学类型'}) 
        return
      }
      if (this.gradeDistributions.length == 0) {
        this.$message({type: 'error', message: '请选择所配年级'}) 
        return
      }
      // if (this.areaData.countyId) {
      //     this.areaId = this.areaData.countyId
      //   } else {
      //     this.areaId = this.areaData.cityId
      //   }
      // if (this.schoolInfo.postalCode.length > 9) {
      //    this.$message({type: 'error', message: '只能输入十位以内的邮政编码'}) 
      //    return
      // }
      if (this.areaData.countyId) {
          this.areaId = this.areaData.countyId
        } else if (this.areaData.cityId) {
          this.areaId = this.areaData.cityId
        } else {
          this.areaId = this.areaData.provinceId
        }
      this.upDataSchool()
    },
    // 获取详情
    getDetail () {
       this.sheng = ''
        this.shi = ''
        this.qu = ''
      this.activeName = 'first'
      this.getSchoolDetail()
    },
    cliSub () {
      if (!this.schoolInfo.stageId) {
          this.$message({
                type: 'error',
                message: '请先选择学段!'
              })
      }
    },
    changeSubject () {
      this.subjectIds = []
      this.getSubject()
    },
    // 获取学科
        getSubject () {
          var params = {
            // schoolId: this.$store.state.common.cookiesObj.schoolId,
            StageId: this.schoolInfo.stageId,
            PageNum: 1,
            PageSize: 1000
          }
          this.$store.dispatch('schoolManage/SubjectsBySchoolId', params).then(res => {
              this.subjectData = res.items
          })
        },
    // 获取学校详情
    getSchoolDetail () {
      var params = {
        id: this.$store.state.schoolManage.schoolId
      }
       this.$store.dispatch('schoolManage/SchoolDetail', params).then(res => {
             this.schoolInfo = res
              for (var obj of this.schoolInfo.areaListDto.areaDetailDtos) {
                var flag = true
                if (obj.parentId == '110100' || obj.parentId == '120100' || obj.parentId == '310100' || obj.parentId == '500000') {
                  flag = false
                }
                if (obj.levelCode == 1 && flag) {
                    this.sheng = obj.id
                    this.$refs.areIsCity.isShowCity = true
                }
                if (obj.levelCode == 2 && flag) {
                    this.shi = obj.id
                    this.$refs.areIsCity.isShowCity = true
                }
                if (obj.levelCode == 3 && !flag) {
                    this.shi = obj.id
                    this.$refs.areIsCity.isShowCity = false
                } 
                 if (obj.levelCode == 3 && flag) {
                    this.qu = obj.id
                    this.$refs.areIsCity.isShowCity = true
                } 
              }
               this.subjectIds = []
              for (var sub of this.schoolInfo.schoolsubjectmapping) {
                  this.subjectIds.push(sub.subjectId)
              }
              this.agentId = null
              for (var agent of this.schoolInfo.agentschoolmapping) {
                  this.agentId = agent.agentId
              }
              this.gradeDistributions = []
              for (var grade of this.schoolInfo.schoolgrademapping) {
                  this.gradeDistributions.push(grade.gradeId)
              }
              this.schoolInfo.schoolTypeCode = this.schoolInfo.schoolTypeCode ? parseInt(this.schoolInfo.schoolTypeCode) : ''
              this.schoolInfo.schoolRunnerTypeCode = this.schoolInfo.schoolRunnerTypeCode ? parseInt(this.schoolInfo.schoolRunnerTypeCode) : ''
              this.schoolInfo.attachedClassTypeCode = this.schoolInfo.attachedClassTypeCode ? parseInt(this.schoolInfo.attachedClassTypeCode) : ''
              this.schoolInfo.psSchoolingLengthCode = this.schoolInfo.psSchoolingLengthCode ? parseInt(this.schoolInfo.psSchoolingLengthCode) : ''
              this.schoolInfo.jhsSchoolingLengthCode = this.schoolInfo.jhsSchoolingLengthCode ? parseInt(this.schoolInfo.jhsSchoolingLengthCode) : ''
              this.schoolInfo.shsSchoolingLengthCode = this.schoolInfo.shsSchoolingLengthCode ? parseInt(this.schoolInfo.shsSchoolingLengthCode) : ''
              if (this.schoolInfo.schoolFoundedYear) {
                 this.foundYear = this.schoolInfo.schoolFoundedYear.substr(0, 4)
              }
              if (this.schoolInfo.anniversaryDate) {
                this.schoolDay = this.schoolInfo.anniversaryDate.substr(0, 10)
              }
              this.schoolInfo.landPropertyRightTypeCode = this.schoolInfo.landPropertyRightTypeCode ? parseInt(this.schoolInfo.landPropertyRightTypeCode) : ''
              this.schoolInfo.boardingTypeCode = this.schoolInfo.boardingTypeCode ? parseInt(this.schoolInfo.boardingTypeCode) : ''
              this.getSubject()
          })
    },
    // 获取地区的数据
      areaObj (data) {
        this.areaData = data
        // if (this.areaData.isZhi) {
        //   this.$refs.areIsCity.isShowCity = false
        // }
      },
        // 修改学校
        upDataSchool () {
          // 代理商
          var agentId = []
          if (this.agentId) {
             if (this.agentId[0].id) {
                this.agentId.forEach(
                    item => {
                        agentId.push({agentId: item.id})
                    }
                )
            } else {
              agentId.push({agentId: this.agentId})
            }
          }
            // 所属年级
            var gradeDistributions = []
            this.gradeDistributions.forEach(item => {
              var grade = {gradeId: item, deleteFlag: true}
                gradeDistributions.push(grade)
            })
            this.gradeDistributions = gradeDistributions
            // 科目
            var subjectIds = []
            this.subjectIds.forEach(item => {
                 var subject = {subjectId: item, deleteFlag: true}
                subjectIds.push(subject)
            })
            this.subjectIds = subjectIds
            // 校庆日 建校年份
            if (this.foundYear) {
               this.schoolInfo.schoolFoundedYear = this.foundYear + '-01-01 00:00:00'
            }
            if (this.schoolDay) {
              this.schoolInfo.anniversaryDate = this.schoolDay + ' 00:00:00'
            }
           this.schoolInfo.agentschoolmapping = agentId
           this.schoolInfo.schoolgrademapping = gradeDistributions
           this.schoolInfo.schoolsubjectmapping = subjectIds
           this.schoolInfo.areaId = this.areaId
           this.schoolInfo.loginName = this.schoolInfo.adminLogin
          console.log(this.schoolInfo)
          this.$store.dispatch('schoolManage/UpdateSchool', this.schoolInfo).then(res => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.$emit('confirmDialog')
          }).catch(res => {
            this.$emit('confirmDialog')
          })
        }
  }
}
</script>
<style scoped>
.selOption{
  display: inline-block;
    width: 235px;
    margin-bottom: 10px;
}
.selOption span{color: red;}
.selCenter{
  display: inline-block;
}
.formWidth{
        display: inline-block;
        width: 215px;
    }
</style>
